package com.jxl.domain.service.impl;

import com.jxl.common.enums.DeletedCodeEnum;
import com.jxl.common.redis.RedisUtil;
import com.jxl.common.util.ContextUtil;
import com.jxl.domain.convert.SubjectLikedBOConverter;
import com.jxl.domain.entity.SubjectLikedBO;
import com.jxl.domain.service.SubjectLikedDomainService;
import com.jxl.infra.basic.entity.SubjectLiked;
import com.jxl.infra.basic.service.SubjectLikedService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 点赞表 领域service实现了
 *
 * @author jxl
 * @since 2024-09-12 21:32:43
 */
@Service
@Slf4j
public class SubjectLikedDomainServiceImpl implements SubjectLikedDomainService {

    @Resource
    private SubjectLikedService subjectLikedService;

    @Resource
    private RedisUtil redisUtil;

    private static final String SUBJECT_LIKED_KEY = "subject.liked";

    private static final String SUBJECT_LIKED_COUNT_KEY = "subject.liked.count";//题目的电子数量

    private static final String SUBJECT_LIKED_DETAIL_KEY = "subject.liked.detail";//那些人点赞哪些题目


    @Override
    public Boolean add(SubjectLikedBO subjectLikedBO) {
        Long subjectId = subjectLikedBO.getSubjectId();
        String likeUserId = ContextUtil.getUserLoginId();
        Integer status = subjectLikedBO.getStatus();

        String detailKey = SUBJECT_LIKED_DETAIL_KEY + "." + subjectId + "." + likeUserId;
        String countKey = SUBJECT_LIKED_COUNT_KEY + "." + subjectId;
        if (1==status) { //电子
            redisUtil.increment(countKey, 1);//题目点赞加一
            redisUtil.set(detailKey, "1");//当前用户点赞加入进去
        } else {
            Integer count = redisUtil.getInt(countKey);
            if (Objects.isNull(count) || count <= 0) {
                return null;
            }
            redisUtil.increment(countKey, -1);
            redisUtil.del(detailKey);
        }

        return null;
    }

    @Override
    public Boolean isLiked(String subjectId, String userId) {
        String detailKey = SUBJECT_LIKED_DETAIL_KEY + "." + subjectId + "." + userId;
        return redisUtil.exist(detailKey);
    }

    @Override
    public Integer getLikedCount(String subjectId) {
        String countKey = SUBJECT_LIKED_COUNT_KEY + "." + subjectId;
        Integer count = redisUtil.getInt(countKey);
        if (Objects.isNull(count) || count <= 0) {
            return 0;
        }
        return redisUtil.getInt(countKey);
    }

    @Override
    public Boolean update(SubjectLikedBO subjectLikedBO) {
        SubjectLiked subjectLiked = SubjectLikedBOConverter.INSTANCE.convertBOToEntity(subjectLikedBO);
        return subjectLikedService.update(subjectLiked) > 0;
    }

    @Override
    public Boolean delete(SubjectLikedBO subjectLikedBO) {
        SubjectLiked subjectLiked = new SubjectLiked();
        subjectLiked.setId(subjectLikedBO.getId());
        subjectLiked.setIsDeleted(DeletedCodeEnum.UN_DELETE.getCode());
        return subjectLikedService.update(subjectLiked) > 0;
    }

}
